/*
*    Clean AJAX Engine v4.0
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
* Provides a facade to all engine features.
* @author Carlos Eduardo Goncalves
*/

/**
* <p>Clean facade constructor.</p>
*/
function Clean(){
}

/**
* <p>Create a <code>Message</code> setting required properties.</p>
* @param url
*		Url to access.
* @param consumer
*		Consumer id.
* @param onError
*		Function that should be called at the onError event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createSimpleMessage = function(url, consumer, onError){
  var msg = new Message();
  msg.address = url;
  msg.consumer = consumer;
  msg.onError = onError;
  return msg;  
} 

/**
* <p>Create a <code>Message</code> setting main properties.</p>
* @param url
*		Url to access.
* @param xslt
* 		Xslt file used to parse the response.
* @param consumer
*		Consumer id.
* @param doRefresh
*		Indicates if the consumer must be refreshed.
* @param onError
*		Function that should be called at the onError event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createMessage = function(url, xslt, consumer, doRefresh, onError){
  var msg = new Message();
  msg.address = url;
  msg.xslt = xslt;  
  msg.refresh = doRefresh;    
  msg.consumer = consumer;
  msg.onError = onError;  
  return msg;  
} 

/**
* <p>Create a <code>Message</code> setting all properties.</p>
* @param url
*		Url to access.
* @param xslt
*  		Xslt file used to parse the response.
* @param consumer
*		Consumer id.
* @param doRefresh
*		Indicates if the consumer must be refreshed.
* @param onError
*		Function that should be called at the onError event.
* @param onChange
*		Function that should be called at the onChange event.
* @param onComplete
*		Function that should be called at the onComplete event.
* @return
*  		New <code>Message</code> instance.
*/
Clean.createFullMessage = function(url, xslt, consumer, doRefresh, onError, onChange, onComplete){
  var msg = new Message();
  msg.address = url;
  msg.xslt = xslt;  
  msg.consumer = consumer;
  msg.refresh = doRefresh;  
  msg.onError = onError; 
  msg.onChange = onChange;   
  msg.onComplete = onComplete;   
  return msg;
} 

/**
* <p>Transport message using HTTP GET method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.doGet = function(msg){
  msg.method = "GET";
  return Connection.sendMessage(msg);  
} 

/**
* <p>Transport message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param value
*		Value to post.
* @param encrypt
*		<code>Boolean</code> flag that defines if post data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.doPost = function(msg, value, encrypt){
  msg.method = "POST";
  if(encrypt) {
    var encryptor = new HashTool();
	msg.value = encryptor.encodeString(value);
  }
  else
    msg.value = value;
  return Connection.sendMessage(msg);  
} 

/**
* <p>Locates a form using its id and transport it inside a 
* message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param formId
*		<code>HTMLForm</code> id attribute.
* @param encrypt
*		<code>Boolean</code> flag that defines if form data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.postFormById = function(msg, formId, encrypt){
  msg.method = "POST";	
  var iterator = new DomIterator(document);
  var form = iterator.getFormById(formId);
  if(form != null)
    return Connection.sendFormByMessage(msg, form, encrypt);
} 

/**
* <p>Locates a form using its name and transport it inside a 
* message using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param formName
*		<code>HTMLForm</code> name attribute.
* @param encrypt
*		<code>Boolean</code> flag that defines if form data must be encrypted.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.postFormByName = function(msg, formName, encrypt){
  msg.method = "POST";		
  var iterator = new DomIterator(document);  
  var form = iterator.getFormByName(formName);
  if(form != null)	
    return Connection.sendFormByMessage(msg, form, encrypt);
} 

/**
* <p>Transport a <code>RemoteMethod</code> instance inside a message 
* using HTTP POST method.</p>
* @param msg
* 		<code>Message</code> instance to transport.
* @param rpc
*		<code>RemoteMethod</code> instance used to identify the web 
*		service to call, including its name and parameters.
* @return 
*		<code>Message</code> id generated by the engine.
*/
Clean.callWebService = function(msg, rpc){
  msg.method = "POST";		
  return Connection.callWebService(msg, rpc);
}